package com.andyliu.sys.service.impl;

import com.andyliu.sys.dto.ResourceDto;
import com.andyliu.sys.dto.ResourceTableDto;
import com.andyliu.sys.mapper.SResourceMapper;
import com.andyliu.sys.pojo.SResource;
import com.andyliu.sys.pojo.SResourceExample;
import com.andyliu.sys.service.ResourceService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * ClassName: ResourceServiceImpl
 * Description: 资源业务
 * date: 2021/1/20 5:16 下午
 *
 * @author andyliu
 * @since JDK 1.8
 */
@Service
public class ResourceServiceImpl implements ResourceService {
    @Autowired
    private SResourceMapper mapper;
    @Override
    public Boolean saveResource(SResource resource) {
        int flag = 0;
        if(resource.getId()!=null){
            flag = mapper.updateByPrimaryKeySelective(resource);
        }else{
            flag = mapper.insertSelective(resource);
        }
        return flag==1?true:false;
    }

    @Override
    public Boolean deleteResourceById(Integer id) {
        int flag = mapper.deleteByPrimaryKey(id);
        return flag==1?true:false;
    }

    @Override
    public SResource selectResourceById(Integer id) {
        return mapper.selectByPrimaryKey(id);
    }

    @Override
    public PageInfo<SResource> selectResourceByPage(SResource resource, Integer pageNumber, Integer pageSize) {
        PageHelper.startPage(pageNumber,pageSize);
        SResourceExample ex = new SResourceExample();
        SResourceExample.Criteria criteria = ex.createCriteria();
        if(resource!=null){
            if(resource.getResourcename()!=null && !"".equals(resource.getResourcename())){
                criteria.andResourcenameLike("%"+resource.getResourcename()+"%");
            }
        }
        List<SResource> resources = mapper.selectByExample(ex);
        return new PageInfo<SResource>(resources);
    }

    @Override
    public Boolean deleteResourceById(Integer[] ids) {
        int flag =0;
        for(Integer id:ids){
            flag+=mapper.deleteByPrimaryKey(id);
        }
        return flag==ids.length?true:false;
    }

    @Override
    public List<ResourceDto> selectResources(Integer roleId) {
        return mapper.selectParents(roleId);
    }

    @Override
    public List<ResourceTableDto> queryForTable(SResource resource) {
        return mapper.queryForTable(resource);
    }

}
